/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.amazon.s3;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 *
 * @author ahmed
 */
public class MacAddress {

    public static void main(String[] args) {
        try {

            InetAddress address = InetAddress.getLocalHost();
            System.out.println("Address " + address);
            /*
             * Get NetworkInterface for the current host and then read the
             * hardware address.
             */
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            System.out.println("NI = " + ni);
            byte[] mac = ni.getHardwareAddress();


            /*
             * Extract each array of mac address and convert it to hexa with the
             * following format 08-00-27-DC-4A-9E.
             */

            System.out.println("MAC Address is ");
            for (int i = 0; i < mac.length; i++) {
                System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
